import { Body, Controller, Post } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { AppArticleService } from './article.service';
import { Article } from './article.entity';
import { ApiRes, ApiResType } from 'src/common/decorator/api-res.decorator';
import { AppArticleListDto } from './dto/list.dto';
import { AppArticleQueryDto } from './dto/query.dto';
import { AddPushArticleDto, TestBatchAddArticleDto } from './dto/add.dto';
import { Public } from 'src/common/decorator/public.decorator';

@ApiTags('app文章模块')
@ApiBearerAuth()
@Controller('app/article')
export class AppArticleController {
  constructor(private readonly appCollectionService: AppArticleService) {}

  @ApiOperation({
    summary: '根据合辑id分页查询文章列表',
  })
  @Post('/list')
  @ApiRes(Article, ApiResType.PAGE_ARRAY)
  @Public()
  async list(@Body() data: AppArticleListDto) {
    return {
      data: await this.appCollectionService.findAll(data),
    };
  }

  @ApiOperation({
    summary: '查询文章id查询文章详情',
  })
  @Post('/query')
  @ApiRes(Article)
  async query(@Body() data: AppArticleQueryDto) {
    return {
      data: await this.appCollectionService.findOne(data.id),
    };
  }

  @Post('/batchAdd')
  @Public()
  async batchAdd(@Body() data: TestBatchAddArticleDto) {
    const list = data.contentList.map((item, index) => {
      return {
        name: item.name,
        gatherId: data.gatherId,
        content: item.content,
        vipLevel: data.vipLevel,
        sortOrder: index + 1,
      };
    });
    // return {
    //   data: data.contentList.map((item, index) => {
    //     return {
    //       name: item.name,
    //       gatherId: data.gatherId,
    //       content: item.content,
    //       vipLevel: data.vipLevel,
    //       sortOrder: index + 1,
    //     };
    //   }),
    // };

    return {
      data: this.appCollectionService.testBatchCreate(list, data.gatherId),
    };
  }

  @Post('/add')
  @Public()
  async add(@Body() data: AddPushArticleDto) {
    return {
      data: this.appCollectionService.create(data),
    };
  }
}
